package com.study.io.bio.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerHandler implements Runnable {

	private Socket socket;
	
	public ServerHandler(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		BufferedReader in = null;
		PrintWriter out = null;
		try {
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(),true);
			String expression;
			//String result;
			while(true){
				if((expression = in.readLine()) == null) break;
				System.out.println("服务器收到消息: " + expression);

				out.println(expression);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(in != null){
				try { 
					in.close();
				} catch(Exception e){
					e.printStackTrace();
				}
			}
			if(out != null){
				out.close();
				out = null;
			}
			if(socket != null){
				try{
					socket.close();
					
				}catch(Exception e) {
					e.printStackTrace();
				}finally {
					socket = null;
				}
			}
		}
		
		
	}

}
